package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;

    @Override
    public void save(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookMapper.save(addressBook);
    }

    @Override
    public List<AddressBook> list() {
        Long userId = BaseContext.getCurrentId();
        return addressBookMapper.list(userId);
    }

    @Override
    public AddressBook def() {

        return addressBookMapper.def(BaseContext.getCurrentId());
    }

    @Override
    public void updAddress(AddressBook addressBook) {
        addressBookMapper.update(addressBook);
    }

    @Override
    public void updDefault(AddressBook addressBook) {
        //先让其它全变为0，不是默认的
        addressBookMapper.updSet0(BaseContext.getCurrentId());
        //在设置单个为默认值
        addressBook.setIsDefault(1);
        addressBookMapper.update(addressBook);
    }

    @Override
    public void del(Long id) {
        addressBookMapper.del(id);
    }

    @Override
    public AddressBook findById(Long id) {
        return addressBookMapper.findById(id);
    }
}
